﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class NhanVienDAO
    {
        public static DataTable DanhSachKH()
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn = DataProvider.Connect();
                string str = "exec sp_DanhSachKhachHang";
                SqlDataAdapter da = new SqlDataAdapter(str, cn);
                da.Fill(dt);
                cn.Close();
            }
            catch (Exception) { }
            return dt;
        }
        public static DataTable KhachHang(int maKH,int LoaiKH)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("sp_KhachHang", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKH", maKH));
                cmd.Parameters.Add(new SqlParameter("@maLoaiKH", LoaiKH));
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                cn.Close();
            }
            catch (Exception) { }

            return dt;
        }
        public static DataTable DSLoaiKH()
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn = DataProvider.Connect();
                string str = "exec sp_DSLoaiKH";
                SqlDataAdapter da = new SqlDataAdapter(str, cn);
                da.Fill(dt);
                cn.Close();
            }
            catch (Exception) { }

            return dt;
        }
        public static DataTable DSTruSo()
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn = DataProvider.Connect();
                string str = "exec sp_DSTruSo";
                SqlDataAdapter da = new SqlDataAdapter(str, cn);
                da.Fill(dt);
                cn.Close();
            }
            catch (Exception) { }

            return dt;
        }
        public static DataTable TimKiemKH(KhachHang kh)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("sp_TimKiemKH", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKH", kh.MaKH));
                cmd.Parameters.Add(new SqlParameter("@ten", kh.Ten));
                cmd.Parameters.Add(new SqlParameter("@DiaChi", kh.DiaChi));
                cmd.Parameters.Add(new SqlParameter("@LoaiKH", kh.MaLoaiKH));
                cmd.Parameters.Add(new SqlParameter("@matruso", kh.MaTruSo));
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
                cn.Close();
            }
            catch (Exception) { }
         
            return dt;
        }
        public static bool CapNhatThongTinKH(KhachHang kh)
        {
            try 
            {
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("sp_CapNhatThongTinKH", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKH", kh.MaKH));
                cmd.Parameters.Add(new SqlParameter("@ten", kh.Ten));
                cmd.Parameters.Add(new SqlParameter("@cmnd", kh.DiaChi));
                cmd.Parameters.Add(new SqlParameter("@passpost", kh.Ten));
                cmd.Parameters.Add(new SqlParameter("@NgaySinh", kh.DiaChi));
                cmd.Parameters.Add(new SqlParameter("@DiaChi", kh.Ten));
                cmd.Parameters.Add(new SqlParameter("@MST", kh.DiaChi));
                cmd.Parameters.Add(new SqlParameter("@GPKD", kh.Ten));
                cmd.Parameters.Add(new SqlParameter("@NguoiDaiDien", kh.DiaChi));
                cmd.Parameters.Add(new SqlParameter("@dienthoai", kh.Ten));
                cmd.ExecuteNonQuery();
                cn.Close();
            }
            catch (Exception) { }
            return true;
        }
        public static bool CapNhatThongTinNhanVienDirtyRead(NhanVien nv)
        {
           // try
          // {
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("Proc_NhanVien_Update", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNV", nv.MaNV));
                cmd.Parameters.Add( new SqlParameter("@ten", nv.TenNV));
                cmd.Parameters.Add(new SqlParameter("@NgaySinh", nv.NgaySinh));
                cmd.Parameters.Add(new SqlParameter("@DiaChi", nv.DiaChi));
                cmd.Parameters.Add(new SqlParameter("@SoDT", nv.DienThoai));
                cmd.Parameters.Add(new SqlParameter("@CMND", nv.CMND));
                cmd.Parameters.Add(new SqlParameter("@MoTa", nv.MoTa));
                cmd.ExecuteNonQuery();
                cn.Close();

           // }
           // catch (Exception) { }
            return true;
        }
        public static List<NhanVien> DSNhanVien(int type)
        {
               
            List<NhanVien> lst = new List<NhanVien>();
            NhanVien result = null;

            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_XemNhanvien", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@Type", type));
            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {

                result = new NhanVien();
                result.MaDangNhap = Convert.ToInt32(dataReader["MaDangNhap"]);

                result.MaNV = Convert.ToInt32(dataReader["MaNV"]);
                result.MaTruSo = Convert.ToInt32(dataReader["MaTruSo"]);
                result.TenNV = Convert.ToString(dataReader["TenNV"]);
                result.NgaySinh = Convert.ToDateTime(dataReader["NgaySinh"]);
                result.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                result.DienThoai = Convert.ToInt32(dataReader["DienThoai"]);
                result.MaLoaiGioiTinh = Convert.ToInt32(dataReader["MaLoaiGioiTinh"]);
                result.CMND = Convert.ToInt32(dataReader["CMND"]);
                result.MoTa = Convert.ToString(dataReader["MoTa"]);
                result.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                lst.Add(result);
            }
            cn.Close();
            return lst;
        }
        public static NhanVien GetByMaDangNhap(int ma)
        {
            NhanVien result = null;

            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_NhanVien_GetByMaDangNhap", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add(new SqlParameter("@maDN", ma));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {

                result = new NhanVien();
                result.MaDangNhap = ma;

                result.MaNV = Convert.ToInt32(dataReader["MaNV"]);
                result.MaTruSo = Convert.ToInt32(dataReader["MaTruSo"]);
                result.TenNV = Convert.ToString(dataReader["TenNV"]);
                result.NgaySinh = Convert.ToDateTime(dataReader["NgaySinh"]);
                result.DiaChi = Convert.ToString(dataReader["DiaChi"]);
                result.DienThoai = Convert.ToInt32(dataReader["DienThoai"]);
                result.MaLoaiGioiTinh = Convert.ToInt32(dataReader["MaLoaiGioiTinh"]);
                result.CMND = Convert.ToInt32(dataReader["CMND"]);
                result.MoTa = Convert.ToString(dataReader["MoTa"]);
                result.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
            }
            cn.Close();
            return result;
        }
        public static bool InsertRutTien(GiaoDich gd)
        {
            try
            {
                SqlConnection cn = DataProvider.Connect();
                SqlCommand cmd = new SqlCommand("Proc_GiaoDich_RutTien", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maTKThe", gd.MaTKThe));
                cmd.Parameters.Add(new SqlParameter("@loaiGD", gd.MaLoaiGD));
                cmd.Parameters.Add(new SqlParameter("@ngayGD", gd.NgayGD));
                cmd.Parameters.Add(new SqlParameter("@diaChi", gd.DiaChiGD));
                cmd.Parameters.Add(new SqlParameter("@soTienGD", gd.SoTienGD));
                cmd.Parameters.Add(new SqlParameter("@noiDung", gd.NoiDungGD));
                cmd.Parameters.Add(new SqlParameter("@maNV", gd.MaNVGD));
                cmd.Parameters.Add(new SqlParameter("@trangThai", gd.TrangThai));
                cmd.ExecuteNonQuery();

                cmd = new SqlCommand("Proc_TaiKhoanThe_UpdateSodu", cn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maTKThe", gd.MaTKThe));
                cmd.Parameters.Add(new SqlParameter("@soDu", gd.SoTienGD));
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) {  }

            return true;
        }
        /////////////Yen lam
        public static bool InsertNhanVien(NhanVien nv, TaiKhoanDangNhapNV taiKhoanDangNhapNhanVien)
        {
            //try
            //{
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("sp_NhanVien_New", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@MaTruSo", nv.MaTruSo));
            cmd.Parameters.Add(new SqlParameter("@TenNV", nv.TenNV));
            cmd.Parameters.Add(new SqlParameter("@TenDangNhap", taiKhoanDangNhapNhanVien.TenDangNhap));
            cmd.Parameters.Add(new SqlParameter("@MatKhau", taiKhoanDangNhapNhanVien.MatKhau));

            cmd.Parameters.Add(new SqlParameter("@NgaySinh", nv.NgaySinh));
            cmd.Parameters.Add(new SqlParameter("@DiaChi", nv.DiaChi));
            cmd.Parameters.Add(new SqlParameter("@DienThoai", nv.DienThoai));
            cmd.Parameters.Add(new SqlParameter("@MaLoaiGioiTinh", nv.MaLoaiGioiTinh));
            cmd.Parameters.Add(new SqlParameter("@CMND", nv.CMND));
            cmd.Parameters.Add(new SqlParameter("@MoTa", nv.MoTa));
            cmd.ExecuteNonQuery();
            cn.Close();
            //} 
            //catch (Exception ex) { }
            return true;
        }
    }
}
